$(document).ready(function() {
	getCatalogory();
	getProductList();
	getCataMenu();
	getKeyword();

})

function getKeyword() {
	$("#keyword").val(getQueryString("keyword"));

}

function getCataMenu() {

	My.ajax1("../api/product/getCatalogyList.jhtml", function fillCatalogies(index) {
		$("#cataMenu").empty();
		for(var i = 0; i < index.length; i++) {
			$("#cataMenu").append("<li> <a href='category.html?cataId=" + index[i].value + "'>" + index[i].name + "</a></li>");
		}
	});

}

/**
 * 初始化左侧栏分类选择框
 */
function getCatalogory() {

	My.ajax1("../api/product/getCatalogyList.jhtml", function fillCatalogies(index) {
		var cat = $("#catalogies").empty();
		$("#catalogies").append("<option value='0'>ALL TYPES</option>");
		for(var i = 0; i < index.length; i++) {
			$("#catalogies").append("<option value='" + index[i].value + "'>" + index[i].name + "</option>");
		}

		cat.change(function(o) {

			if(this.value == 0) {
				My.ajax1("../api/product/getTypeList.jhtml", function fillTypes(index) {
					$("#types").empty();
					for(var i = 0; i < index.length; i++) {
						$("#types").append("<label class='checkbox'><a  href='category.html?cataId=" + index[i].cataId + "&typeId=" + index[i].value + "'>" + index[i].name + "</label>");
					}

				});
				return;
			}

			var cataId = this.value;

			var params = {
				"cata_id": cataId
			};

			My.ajax2("../api/product/getTypeList.jhtml", params, function fillTypes(index) {
				$("#types").empty();
				for(var i = 0; i < index.length; i++) {
					$("#types").append("<label class='checkbox'><a  href='category.html?cataId=" + params.cata_id + "&typeId=" + index[i].value + "'>" + index[i].name + "</label>");
				}

			})

		});

	});

	My.ajax1("../api/product/getTypeList.jhtml", function fillTypes(index) {
		$("#types").empty();
		for(var i = 0; i < index.length; i++) {
			$("#types").append("<label class='checkbox'><a  href='category.html?cataId=" + index[i].cataId + "&typeId=" + index[i].value + "'>" + index[i].name + "</label>");
		}

	});

}

function getProductList(cataId, typeId) {

	var cataId = getQueryString("cataId");

	var typeId = getQueryString("typeId");

	var keyword = getQueryString("keyword");

	$("#catalogies option[value='" + cataId + "']").attr("selected", "true");

	var params = {
		"cata_id": cataId,
		"type_id": typeId,
		"product_name": keyword
	};

	My.ajax2("../api/product/getProductList.jhtml", params, function fillTypes(index) {

		for(var i = 0; i < index.length; i++) {
			var prod = "prod" + (i + 1);
			var aUrl = "aUrl" + (i + 1);
			var mainUrl = "mainUrl" + (i + 1);
			var prodName = "prodName" + (i + 1);
			var price = "price" + (i + 1);
			$("#" + aUrl).attr('href', 'product.html?id=' + index[i].product_id);
			$("#" + mainUrl).attr('href', 'product.html?id=' + index[i].product_id);
			$("#" + mainUrl).attr('src', picUrl + index[i].main_url);
			$("#" + prodName).text(index[i].product_name);
			$("#" + prodName).attr('href', 'product.html?id=' + index[i].product_id);

			$("#" + price).remove();
			$("#" + price).append("$" + index[i].discount_price + "<span>$" + index[i].full_price + "</span>");

		}
	})

}